﻿namespace Chalk.Extension.Tests
{
    #region Directives

    using System;
    using System.Text;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Graffiti.Core;

    using Microsoft.VisualStudio.TestTools.UnitTesting.Web;

    #endregion

    /// <summary>
    /// Series of tests to ensure custom helper methods function as expected.
    /// </summary>
    [TestClass]
    public class ExtensionsTests
    {
        #region Fields

        private ChalkExtensions chalkExtensions = new ChalkExtensions();

        private TestContext testContextInstance;

        #endregion

        #region Constructor

        public ExtensionsTests() { }

        #endregion

        #region Properties

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        #endregion

        [TestMethod]
        public void GetAllPostsForBlogCategoryShouldReturnCachedThreePosts()
        {
            PostCollection posts = chalkExtensions.GetAllPostsForCategory("Blog");

            Assert.AreEqual<int>(3, posts.Count);
            Assert.AreEqual<string>("Study: 73% of percentages are made up", posts[0].Title);
            Assert.AreEqual<string>("Leopard user can't get over control+c", posts[1].Title);
            Assert.AreEqual<string>("Carbon Economy Might Benefit Health", posts[2].Title);

            //ensure that caching is working by calling the same method with a different parameter and
            //testing to see if returned data is different.
            posts = chalkExtensions.GetAllPostsForCategory("Sample Content");

            Assert.AreEqual<int>(2, posts.Count);
            Assert.AreEqual<string>("A Little Different", posts[0].Title);
            Assert.AreEqual<string>("A Shorter Post", posts[1].Title);
        }

        [HostType("ASP.NET")]
        [UrlToTest("http://benchpeg.net")]
        [TestMethod]
        public void GetAllTagsForCategoryShouldReturnUniqueList()
        {
            string content = chalkExtensions.GetAllTagsForCategory("Looking");

            Assert.IsTrue(content.Length > 10);
        }
        [TestMethod]
        public void GetNextPostForCategoryShouldReturnValidPost()
        {
            PostCollection allPostsInCategory = chalkExtensions.GetAllPostsForCategory("Blog");

            Post nextPost = chalkExtensions.GetNextPostForCategory("Blog", allPostsInCategory[0]);

            Assert.AreEqual<string>("Leopard user can't get over control+c", nextPost.Title);
        }

        [TestMethod]
        public void GetNextPostForCategoryShouldReturnNullWhenPostIsLastPostInCategory()
        {
            PostCollection allPostsInCategory = chalkExtensions.GetAllPostsForCategory("Blog");

            Post nextPost = chalkExtensions.GetNextPostForCategory("Blog", allPostsInCategory[2]);

            Assert.IsNull(nextPost);
        }

        [TestMethod]
        public void GetPreviousPostForCategoryShouldReturnValidPost()
        {
            PostCollection allPostsInCategory = chalkExtensions.GetAllPostsForCategory("Blog");

            Post previousPost = chalkExtensions.GetPreviousPostForCategory("Blog", allPostsInCategory[2]);

            Assert.AreEqual<string>("Leopard user can't get over control+c", previousPost.Title);
        }

        [TestMethod]
        public void GetPreviousPostForCategoryShouldReturnNullWhenPostIsFirstPostInCategory()
        {
            PostCollection allPostsInCategory = chalkExtensions.GetAllPostsForCategory("Blog");

            Post previousPost = chalkExtensions.GetPreviousPostForCategory("Blog", allPostsInCategory[0]);

            Assert.IsNull(previousPost);
        }

        /// <summary>
        /// Not entirely sure how to test for randomness!
        /// </summary>
        [TestMethod]
        public void GetRandomPostsForBlogCategoryShouldReturnOnePost()
        {
            PostCollection posts = chalkExtensions.GetRandomPostsForCategory("Blog", 1);

            Assert.AreEqual<int>(1, posts.Count);
        }

        [TestMethod]
        public void SortPostsAscendingShouldReturnPostNamedCarbonEconomyPostFirst()
        {
            PostCollection posts = chalkExtensions.GetAllPostsForCategory("Blog");

            chalkExtensions.SortPostsAscending(posts, "Title");

            Assert.AreEqual<int>(3, posts.Count);
            Assert.AreEqual<string>("Carbon Economy Might Benefit Health", posts[0].Title);
        }

        [TestMethod]
        public void SortPostsDescendingShouldReturnStudy73PercentFirst()
        {
            PostCollection posts = chalkExtensions.GetAllPostsForCategory("Blog");

            chalkExtensions.SortPostsDescending(posts, "Title");

            Assert.AreEqual<int>(3, posts.Count);
            Assert.AreEqual<string>("Study: 73% of percentages are made up", posts[0].Title);
        }
    }
}